package com.spark.gaea.utils;

import java.util.Random;

public class StringGetUtil {
	public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String NUMBERCHAR = "0123456789";

	public static String generateString(final int length) {
		final StringBuffer sb = new StringBuffer();
		final Random random = new Random();
		for (int i = 0; i < length; ++i) {
			sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
					.charAt(random.nextInt("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".length())));
		}
		return sb.toString();
	}

	public static String generateMixString(final int length) {
		final StringBuffer sb = new StringBuffer();
		final Random random = new Random();
		for (int i = 0; i < length; ++i) {
			sb.append("abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
					.charAt(random.nextInt("abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".length())));
		}
		return sb.toString();
	}

	public static String generateNumber(final int length) {
		final StringBuffer sb = new StringBuffer();
		final Random random = new Random();
		for (int i = 0; i < length; ++i) {
			sb.append("0123456789".charAt(random.nextInt("0123456789".length())));
		}
		return sb.toString();
	}

	public static String generateLowerString(final int length) {
		return generateMixString(length).toUpperCase();
	}

	public static String generateUpperString(final int length) {
		return generateMixString(length).toLowerCase();
	}

	public static String generateZeroString(final int length) {
		final StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; ++i) {
			sb.append('0');
		}
		return sb.toString();
	}

	public static String toFixdLengthString(final long num, final int fixdlenth) {
		final StringBuffer sb = new StringBuffer();
		final String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0) {
			sb.append(generateZeroString(fixdlenth - strNum.length()));
			sb.append(strNum);
			return sb.toString();
		}
		throw new RuntimeException("\u5c06\u6570\u5b57" + num + "\u8f6c\u5316\u4e3a\u957f\u5ea6\u4e3a" + fixdlenth
				+ "\u7684\u5b57\u7b26\u4e32\u53d1\u751f\u5f02\u5e38\uff01");
	}

	public static int getNotSimple(final int[] param, final int len) {
		final Random rand = new Random();
		for (int i = param.length; i > 1; --i) {
			final int index = rand.nextInt(i);
			final int tmp = param[index];
			param[index] = param[i - 1];
			param[i - 1] = tmp;
		}
		int result = 0;
		for (int j = 0; j < len; ++j) {
			result = result * 10 + param[j];
		}
		return result;
	}

	public static String jointString(final String benginString, final long endNum, final int fixdlenth) {
		final StringBuffer sb = new StringBuffer();
		final String strNum = String.valueOf(endNum);
		sb.append(benginString.trim());
		if (fixdlenth - benginString.trim().length() - strNum.length() >= 0) {
			sb.append(generateZeroString(fixdlenth - benginString.trim().length() - strNum.length()));
			sb.append(strNum);
			return sb.toString();
		}
		throw new RuntimeException("\u5c06\u6570\u5b57" + endNum + "\u8f6c\u5316\u4e3a\u957f\u5ea6\u4e3a" + fixdlenth
				+ "\u7684\u5b57\u7b26\u4e32\u53d1\u751f\u5f02\u5e38\uff01");
	}
}
